$(document).ready(function () {
	var phone_reg = /(^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$)|(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;//姓名/手机号 手机号验证

	//获取href键值对
	function getRequest(){
		var str=location.search; //  ?userName=zhanghao$userId=123
		if(str){
			var url=str.substr(1),arr=url.split('&'),len=arr.length,i= 0,request={};
			for(;i<len;i++){
				request[arr[i].split('=')[0]]=arr[i].split('=')[1];
			}
			return request;
		}else{
			console.log('没有传递参数');
		}
	}

	//选择城市
	select_city();
	function select_city() {
		var select_city = $('#select_city');
		var city = city_data_3;
		var select_city_input = $('#select_city_input');

		var picker = new mui.PopPicker({layer: 3}); //创建选择器
		picker.setData( city );//设置数据
		select_city.on('click',function () {
			picker.show(function (data) {
				var text = data[0].text + ' ' + data[1].text + ' ' + data[2].text;
				select_city_input.val( text );
				select_city.find('.pull-right').html('<span style="line-height: 1.8;">'+ text +'</span> ' + '<img src="/statics/images/right.png" alt="">');
			});
		});
	}

	//获取产品信息
	get_jollyone_info();
	function get_jollyone_info() {
		var goods_info = $('#goods_info');
		$.ajax({
			url: '/Jolly/Jolly_list',
			type:'POST',
			dataType: 'json',
			data: {
				jolly_id: getRequest().project_id
			},
			success:function (data) {
				if( data.code == '1' ){
					goods_info.find('img').attr( 'src' , data.data[0].jolly_face_img );
					$('#goods_info_worth').html( '积分<span>'+ data.data[0].jolly_price.split(".")[0] +'</span>.' + data.data[0].jolly_price.split(".")[1] );

					$('#sum_money').text( parseFloat( data.data[0].jolly_price ).toFixed(2) );//小计
					$('#footer_sum').html( data.data[0].jolly_price.split(".")[0] + '<span>.'+ data.data[0].jolly_price.split(".")[1] +'</span>' );//合计

					add_subtract_num( data.data[0].jolly_price );//加减购买数量
					limit_input_num( data.data[0].jolly_price );//限制输入为数字(输入购买数量)
				}else {
					alert(data.msg);
				}
			},
			error:function (err) {
				console.log(err);
			}
		})
	}

	//加减购买数量
	function add_subtract_num(univalence) {
		var subtract = $('#subtract');
		var add = $('#add');
		var buy_num = $('#buy_num');
		var goods_num = $('#goods_num');

		if( buy_num.val() > 1 ){
			subtract.removeClass('active');
		}else if(buy_num.val() <= 1) {
			subtract.addClass('active');
		}

		subtract.on('click',function () {
			if( subtract.hasClass('active') ){
				buy_num.val( '1' );
			}else {
				buy_num.val( parseInt( buy_num.val() ) - 1 );
			}

			need_pay_integral( buy_num.val() , univalence );//计算所需金额(积分)  并展示在页面上

			if( buy_num.val() > 1 ){
				subtract.removeClass('active');
			}else if(buy_num.val() <= 1) {
				subtract.addClass('active');
			}

		});

		add.on('click',function () {
			buy_num.val( parseInt( buy_num.val() ) + 1 );

			need_pay_integral( buy_num.val() , univalence );//计算所需金额(积分)  并展示在页面上

			if( buy_num.val() > 1 ){
				subtract.removeClass('active');
			}else if(buy_num.val() <= 1) {
				subtract.addClass('active');
			}

		});
	}

	//限制输入为数字(输入购买数量)
	function limit_input_num(univalence) {
		var buy_num = $('#buy_num');
		buy_num.on('keyup',function () {
			$(this).val( $(this).val().replace(/[^0-9]/ig,'') );

			if( $(this).val() == '' || $(this).val() == null || $(this).val() == undefined ){
				$(this).val('1')
			}

			need_pay_integral( buy_num.val() , univalence );//计算所需金额(积分)  并展示在页面上
		});
	}

	//计算所需金额(积分)  并展示在页面上
	function need_pay_integral(num,univalence) {
		var goods_num = $('#goods_num');//x1
		var sum_num = $('#sum_num');//小计
		var sum_money = $('#sum_money');//小计
		var footer_sum = $('#footer_sum');//合计

		goods_num.text( 'x' + num );//x1
		sum_num.text( '共' + num + '件' );//小计
		sum_money.text( parseFloat( univalence * num ).toFixed(2) );//小计
		footer_sum.html( (univalence * num).toFixed(2).split(".")[0] + '<span>.'+ (univalence * num).toFixed(2).split(".")[1] +'</span>' );//合计
	}

	//必填字段提示
	function must_write_tip() {
		var form = $('#info_list_form');
		var input = form.find('input');
		var modal = $('#myModal');

		if( !input.eq(1).val() ){
			modal.find('.modal-body').text('请您填写收货人姓名');
			modal.modal();
			return false;
		}else if( !input.eq(2).val() ){
			modal.find('.modal-body').text('请您填写收货人手机号');
			modal.modal();
			return false;
		}else if( input.eq(2).val() && !phone_reg.test( input.eq(2).val() ) ){
			modal.find('.modal-body').text('请您正确的填写11位手机号');
			modal.modal();
			return false;
		}else if( !input.eq(3).val() ){
			modal.find('.modal-body').text('请您选择城市');
			modal.modal();
			return false;
		}else if( !input.eq(4).val() ){
			modal.find('.modal-body').text('请您填写详细地址');
			modal.modal();
			return false;
		}else {
			form.find('input[name="jolly_id"]').val( getRequest().project_id );
			form.find('input[name="receive_address"]').val( $('#select_city').find('span').text() + ' ' + $('#write_address_input').val() );
			return true;
		}
	}

	//点击购买  提交订单
	click_at_buy();
	function click_at_buy() {
		var at_buy = $('#at_buy');
		var modal = $('#myModal');
		at_buy.on('click',function () {
			if( must_write_tip() ){
				$.ajax({
					url: '/Jolly/make_order',
					type:'POST',
					dataType: 'json',
					data: $('#info_list_form').serialize(),
					success:function (data) {
						if( data.code == '1' ){
							modal.find('.modal-body').text('下单成功！');
							modal.modal();

							modal.on('hide.bs.modal',function () {
								window.location = '/index/user_order';
							});
						}
					},
					error:function (err) {
						console.log(err);
					}
				})
			}
		});
	}


	/************************************返回上一页***************************/
	//返回上一页
	function go_back() {
		history.go(-1);
	}

	//点击返回键  返回上一页
	click_back_btn();
	function click_back_btn() {
		$('#go_back').on('click',function () {
			go_back();//返回上一页
		});
	}
});